<?php
/*
Template Name: Index page Products Grid
*/
?>

<?php get_header(); ?>

<?php
$aOptions = SofaSuppaStore::initOptions( false );
$prod_description_trim = ( strlen( $aOptions[ 'prod_description_trim' ] ) > 0 ) ? stripslashes( $aOptions[ 'prod_description_trim' ] ) : 0;
$prod_use_description = $aOptions[ 'prod_use_description' ];
$entrycurrency = $aOptions[ 'entrycurrency' ];
// layout
$hp_layout = $aOptions[ 'hp_layout' ]; // 'default', 'wide', 'narrow_scrolled', 'narrow_grided'
$hp_num_products = ( strlen( $aOptions[ 'hp_num_products' ] ) > 0 ) ? intval( $aOptions[ 'hp_num_products' ] ) : 6;
$hp_custom_show_title   = $aOptions[ 'hp_custom_show_title' ];
$hp_custom_show_content = $aOptions[ 'hp_custom_show_content' ];
?>

<!-- main content start -->

	<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
    
    <div id="featured" class="castshadow grid_8">
    
    	<!-- title (if used) start -->
    
		<?php if( $hp_custom_show_title == 'yes' ) { ?>
        <h1><?php the_title(); ?></h1>
        <?php } ?>
        
        <!-- title (if used) end -->
        
        <!-- page content (if used) start -->
        
        <?php if( $hp_custom_show_content == 'yes' ) { ?>
        <?php the_content(); ?>
        <?php } ?>
        
        <!-- page content (if used) end -->

		<?php
        // query products
		global $wp_query;
		$args = array(
			'post_type' => 'product',
			'meta_key' => 'featured',
			'meta_value' => 'on',
			'order' => 'DESC',
			'post_status' => 'publish',
			'posts_per_page' => $hp_num_products 
		);
		
		$temp_query = $wp_query;
		$wp_query = NULL;
		$wp_query = new WP_Query( $args );
        ?>
        
        <?php if ( have_posts() ) : ?>
        
        <?php
        // different classes are used!
        $switch_class = '';
        $count_rows  = 0;
        ?>
        
        <!-- wordpress sub-loop loop start -->
    
    	<?php while ( have_posts() ) : the_post(); ?>
    
    	<?php
		$breakrow = FALSE;
		// switch classes
		if( ( $count_rows + 1 ) % 4 == 0 ) {
			$switch_class = ' omega'; 
			$breakrow = TRUE;
		} elseif( $count_rows % 4 == 0 ) $switch_class = ' alpha';
		else $switch_class = '';
		// get product meta
		$product_meta_price = get_post_meta( get_the_ID(), 'sofa_0_textarea_price', true );
		$product_meta_labeltype = get_post_meta( get_the_ID(), 'labeltype', true );
		$product_meta_labelvalue = get_post_meta( get_the_ID(), 'labelvalue', true );
		?>
    
        <div class="grid_2 product<?php echo $switch_class; ?>">
        
            <?php
			// extract image
			if( has_post_thumbnail( get_the_ID() ) ) {
				$default_attr = array( 'class' => "size-thumbnail alignleft", 'alt' => get_the_title(), 'title' => get_the_title() );
				if( $product_meta_labeltype != '' && $product_meta_labelvalue != '' ) echo '<a href="' . get_permalink( get_the_ID() ) . '" rel="' . $product_meta_labeltype . '|' . $product_meta_labelvalue . '">';
				else echo '<a href="' . get_permalink( get_the_ID() ) . '">';
				echo get_the_post_thumbnail( get_the_ID(), 'thumbnail', $default_attr );
				echo '</a>';
			}
			?>
            
        	<span class="prodtitle"><?php the_title(); ?></span>
            
            <div class="prodinfo">
            
            	<span class="prodprice"><?php echo $entrycurrency . ' ' . $product_meta_price; ?></span>
                <span class="addtocart">
                	<a href="<?php echo get_permalink( $post->ID ); ?>" title="<?php _e( "Buy Now", "sofa_suppastore" ); ?>" rel="nofollow"><?php _e( "Buy Now", "sofa_suppastore" ); ?></a>
                </span>
            
            </div>
            
            <?php
			if( $prod_use_description == 'yes' ) {
				$c_post_perma = get_permalink( $post->ID );
				echo '<p>' . trim_the_content( get_the_excerpt(), __( "...READ MORE", "sofa_suppastore" ), $c_post_perma, (int)$prod_description_trim ) . '</p>';
			}
			?>
            
        </div>
        
        <?php 
		if( $breakrow ) echo '<div class="clear">&nbsp;</div>';
		$count_rows++; 
		?>
        
        <?php endwhile; ?>
        
        <!-- wordpress sub-loop loop end -->
    
    <?php else: ?>
    
    <!-- no content start -->
    
    <div class="grid_8 alpha omega">
        <h1><?php _e( "Nothing found!", "sofa_suppastore" ); ?></h1>
        <p><?php _e( "Sorry but no products found. Take your time, there's gonna be soon to see something listed here.", "sofa_suppastore" ); ?></p>
    </div>
    
    <!-- no content end -->
    
	<?php endif; ?>

    
    <?php
	// backup query aka reset custom query posts loop
	$wp_query = $temp_query;
	$temp_query = NULL;
	?>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    
</div>
    
<!-- main content end -->

<!-- include sidebar start -->

    <?php get_template_part( 'sidebar_index' ); ?>
    
<!-- include sidebar end -->

<?php get_footer(); ?>

